<?php
/**
 * 获取
 *
 *
 */

use Workerman\Worker;
use Workerman\Mysql;

class get_button_csv{

    public function entry($param){
       // var_dump(11111111);die;
        return $this->do_get_button_csv($param);
    }


    /**
     * do_set_poster_contents
     *
     *
     * @access private
     * @param $param
     * @since 1.0
     * @return array(errcode,errmsg,rtv)
     */
    private function do_get_button_csv($param){
        $poster_contents_id = $_POST["poster_contents_id"];
        $page = $_POST["page"];
        $department_id = $_POST["department_id"];

        if(!empty($department_id)){
            $sql = "SELECT b.picture_path,c.id AS picture_department_id,IFNULL(d.name,c.user_id) as user_name,IFNULL(e.name,c.deputy_director) as deputy_director_name,IFNULL(f.name,c.chief_inspector) as chief_inspector_name,c.contents,c.contents_confirm FROM hf_mes_poster_contents a 
                    INNER JOIN hf_mes_poster_picture b ON a.id=b.poster_contents_id 
                    INNER JOIN hf_mes_poster_picture_department c ON b.id=c.picture_id 
                    LEFT JOIN hf_mes_user d ON d.id=c.user_id
                    LEFT JOIN hf_mes_user e ON e.id=c.deputy_director 
                    LEFT JOIN hf_mes_user f ON f.id=c.chief_inspector 
                    WHERE a.id='$poster_contents_id' AND  b.page='$page' 
                    AND c.department_id='$department_id'";

            $dp=sql_query($sql);

            $dp[0]['contents'] = json_decode($dp[0]['contents']);

        }else{
            $sql = "SELECT b.picture_path,c.id AS picture_department_id,IFNULL(d.name,c.user_id) as user_name,IFNULL(e.name,c.deputy_director) as deputy_director_name,IFNULL(f.name,c.chief_inspector) as chief_inspector_name,c.contents,c.contents_confirm FROM hf_mes_poster_contents a 
                    INNER JOIN hf_mes_poster_picture b ON a.id=b.poster_contents_id 
                    INNER JOIN hf_mes_poster_picture_department c ON b.id=c.picture_id 
                    LEFT JOIN hf_mes_user d ON d.id=c.user_id
                    LEFT JOIN hf_mes_user e ON e.id=c.deputy_director 
                    LEFT JOIN hf_mes_user f ON f.id=c.chief_inspector 
                    WHERE a.id='$poster_contents_id' AND  b.page='$page'";

            $dp=sql_query($sql);
            //var_dump($dp);
            $data = [];

            $contents = [];
            if(count($dp) > 0){
                foreach ($dp as $k=>$v){
                    $data[0] = $v;
                    if(!empty($v['contents'])){
                        $arr = json_decode($v['contents'],true);
                        foreach ($arr as $kk=>$vv){
                            array_push($contents,$vv);
                        }
                    }

                }

                if(!empty($contents)){
                    $data[0]['contents'] = $contents;
                }else{
                    $data[0]['contents'] = NULL;
                }
                //var_dump($data);
                $dp = $data;
            }
        }

        if(!empty($dp[0]['contents'])){
            foreach ($dp[0]['contents'] as $v){
                $info[] = $v->info;
            }

            // CSV对应的字段名
            $csv_val = array( 'sku', 'unit', 'price', 'name', 'desc', 'page', 'idx', 's_price', 'limit', 'area_time', 'mark', 'origin', 'flag','department_name');
            //var_dump($info);die;
            $url = $this->DownloadData(date("Y-m-d"),$csv_val,$info);
            //var_dump($url);
        }else{
            return array("errcode"=>0, "errmsg"=>"", "rtval"=>"");
        }

    }

    /**  如果大量数据导出 支持分页写入

     * [DownloadDate 公共导出csv]

     * @param  string                   $name   [文件名称]

     * @param  array                   $header [表头]

     * @param  array                    $data   [数据集]

     * @param                      $is_header   [真 假 是否取表头 解决循环写入问题]

     * @return array             name[文件名称]   filePath[文件路径]

     */

    public static function DownloadData($name='',$header=array(),$data=array(),$is_header=true){

        set_time_limit(0);

        ini_set('memory_limit','2048M');

        header("Content-type:application/vnd.ms-excel");

        header("content-Disposition:filename=downloaded.pdf ");

        try {

            if (!$name || !$data) {

                //throw new BadRequestHttpException('参数不可为空');
                return false;
            }

            $filePath = "./temp/download/{$name}.csv";

            $header = implode(",",$header);

            $header = iconv('UTF-8', 'GBK//IGNORE', $header);

            $header = explode(",", $header);

            $fp = fopen($filePath, 'a+');

            if (!empty($header) && is_array($header) && $is_header) {

                fputcsv($fp, $header);

            }

            foreach ($data as $row) {

                $str = implode("@@@@",$row);

                $str = iconv('UTF-8', 'GBK//IGNORE', $str);

                $str = str_replace(",","|",$str);

                $row = explode("@@@@", $str);

                fputcsv($fp, $row);

            }

            unset($data);

            if(ob_get_level()>0){

                ob_flush();

            }

            flush();

        } catch (Exception $e) {
            return false;
            //throw new BadRequestHttpException($e->getMessage());//抛出异常

        }

        return ltrim($filePath,"./");
/*
        return [

            'filePath'=>

            'name'=>$name.'.csv',

        ];*/

    }

    /**
     * create
     * 通过 call_user_func 获得本class的实例
     *
     * @access public
     * @since 1.0
     * @return object 本类的实例
     */
    public static function create(){
        static $instance;
        if (!$instance){
            $instance = new get_button_csv();
        }
        return $instance;
    }
}


?>